package com.xtayfjpk.zookeeper.test.master_workers;

import java.util.ArrayList;
import java.util.List;

public class ChildrenCache {
	private List<String> children;
	
	public ChildrenCache(){}
	
	public ChildrenCache(List<String> children) {
		this.children = children;
	}
	
	public List<String> getChildren() {
		return children;
	}
	
	public List<String> addedAndSet(List<String> newChildren) {
		ArrayList<String> diff = null;
		if(children==null) {
			diff = new ArrayList<>(newChildren);
		} else {
			for(String newChild : newChildren) {
				if(!children.contains(newChild)) {
					if(diff==null) {
						diff = new ArrayList<>();
					}
					diff.add(newChild);
				}
			}
		}
		this.children = newChildren;
		return diff;
	}
	
	public List<String> removedAndSet(List<String> newChildren) {
        List<String> diff = null;
            
        if(children != null) {
            for(String child: children) {
                if(!newChildren.contains( child )) {
                    if(diff == null) {
                        diff = new ArrayList<String>();
                    }
                    
                    diff.add(child);
                }
            }
        }
        this.children = newChildren;
        
        return diff;
    }
}
